﻿using UnityEngine;
using System.Collections;

public class MotherShipController : MonoBehaviour {
	private int moveX;
	private int moveY;
	private int tieBreaker;
	private GameObject planet;
	public float speed = 8f;
	private Transform enemyShip;
	public Transform playerShip;
	public AudioClip explosionSound;
	public int impactDamage = 50;
	public int mShipHealth = 10;
	private int timesHit = 0;
	
	private GameObject pRef;
	
	// holds the distance between the ship and the player
	private float distance;
	
	// holds the rotation adjustment for the ship to face the player
	private Vector3 targetRot;
	private Quaternion tempRot;
	
	// the vector that the ship partially rotates around to attack the player
	private Vector3 attackVector;
	
	// total degrees in which the ship has rotated around the attack vector
	private int tDeg;
	
	// holds the direction the ship was headed in toward the player
	// before entering attacking state
	private Vector3 direction;
	
	public float fireRate;
	private float nextFire;
	
	public Transform weaponShotPosition;
	public GameObject weaponShot;
	public GameObject planetShot;
	
	private bool attacked = false;
	private bool dead = false;
	
	// ship AI states
	public enum States
	{
		asleep = 0,
		following = 1,
		attacking = 2,
		attackingPlr = 3
	}
	
	// define States as a var and set default to asleep
	public States currentState = States.asleep;
	
	// Update is called once per frame
	void Start(){
		//set the transform data to enemyShip var
		enemyShip = transform;
		planet = GameObject.FindGameObjectWithTag("Planet");
		currentState = States.attacking;
	}
	
	void Update () {
		
		if (!dead)
		{
			/*//Initiate movement direction
			transform.Translate (new Vector3 (moveX, 0, moveY) * speed * Time.deltaTime);
			*/
				// get our player references
				pRef = GameObject.FindGameObjectWithTag("Player");
			
			switch (currentState)
			{
				// do nothing if the ship is asleep
			case States.asleep:
				break;
			case States.attacking:
				direction = (planet.transform.position - this.transform.position);
				distance = Vector3.Distance(this.transform.position, planet.transform.position);
				
				direction.z = 0;
				direction *= 20;
				
				if (!attacked)
				{
					if (distance < 20)
						this.transform.position = Vector3.MoveTowards(this.transform.position, -direction, Time.deltaTime * speed);
					else if (distance > 25)
						this.transform.position = Vector3.MoveTowards(this.transform.position, direction, Time.deltaTime * speed);
				}
				else
				{
					if (tDeg < 360)
					{
						// increment the total degrees
						tDeg += 1;
						this.transform.RotateAround(pRef.transform.position, Vector3.back, (speed * 8) * Time.deltaTime);
					}
					else
					{
						attacked = false;
						tDeg = 0;
					}
				}
				
				if (Time.time > nextFire)
				{
					nextFire = Time.time + fireRate;	
					Instantiate(planetShot, weaponShotPosition.position, enemyShip.rotation);			
				}
				
				break;
			}

			this.transform.LookAt(new Vector3(planet.transform.position.x, planet.transform.position.y, this.transform.position.z), Vector3.back);
			
			///Wrap enemy ship around screen on exit
			if(enemyShip.position.x >= 38)
			{
				enemyShip.position = new Vector3(-38, enemyShip.position.y, enemyShip.position.z);
			}
			else if(enemyShip.position.x <= -38)
			{
				enemyShip.position = new Vector3(38, enemyShip.position.y, enemyShip.position.z);
			}
			else if(enemyShip.position.y >= 20)
			{
				enemyShip.position = new Vector3(enemyShip.position.x,-20, enemyShip.position.z);
			}
			else if(enemyShip.position.y <= -20)
			{
				enemyShip.position = new Vector3(enemyShip.position.x,20, enemyShip.position.z);
			}
		}
	}
	
	void OnTriggerEnter(Collider other){
		if (other.tag == "Player") {
			GameManager.playerHealth-=impactDamage;//Cause damage to the player on impact
					
			mShipHealth--;
			
			if (!attacked)
			{
				nextFire = Time.time + 1;
				attacked = true;
			}
			
			if (mShipHealth <= 0)
			{
				audio.PlayOneShot(explosionSound);
				dead = true;
				renderer.enabled = false;
				collider.enabled = false;
				Destroy(this.gameObject, explosionSound.length);
			}
		}
		if (other.tag == "Bullet") {
			mShipHealth--;

			if (++timesHit > 4 && !attacked)
			{
				timesHit = 0;
				nextFire = Time.time + 1;
				attacked = true;
			}
			else if (timesHit == 5)
				timesHit = 0;

			Destroy(other.gameObject,0);
			
			if (mShipHealth <= 0)
			{
				audio.PlayOneShot(explosionSound);
				dead = true;
				renderer.enabled = false;
				collider.enabled = false;
				Destroy(this.gameObject,explosionSound.length);
			}
		}
	}
}
